//
//  NormalCollectionViewDataSource.m
//  MirrorCharge
//
//  Created by dllo on 16/10/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NormalCollectionViewDataSource.h"
#import "BaseModel.h"
#import "BaseCollectionViewCell.h"

@interface NormalCollectionViewDataSource ()

@end

@implementation NormalCollectionViewDataSource

- (instancetype)initWithSourceArray:(NSArray *)sourceArray sourceArrayType:(NormalCollectionViewType)type configCellBlock:(ConfigCell)configCell
{
    return [self initWithSourceArray:sourceArray sourceArrayType:type reviseIndexPath:nil reviseReuses:nil configCellBlock:configCell];
}

- (instancetype)initWithSourceArray:(NSArray *)sourceArray sourceArrayType:(NormalCollectionViewType)type reviseIndexPath:(NSArray<NSIndexPath *> *)reviseIndexPaths reviseReuses:(NSArray<NSString *> *)reviseReuses configCellBlock:(ConfigCell)configCell {
    return [self initWithSourceArray:sourceArray sourceArrayType:type reviseIndexPath:reviseIndexPaths reviseReuses:reviseReuses configCellBlock:configCell configHeaderOrFooterView:nil];
}

- (instancetype)initWithSourceArray:(NSArray *)sourceArray sourceArrayType:(NormalCollectionViewType)type reviseIndexPath:(NSArray<NSIndexPath *> *)reviseIndexPaths reviseReuses:(NSArray<NSString *> *)reviseReuses configCellBlock:(ConfigCell)configCell configHeaderOrFooterView:(ConfigHeaderOrFooterView)configHeaderOrFooterView {
    self = [super init];
    if (self) {
        _configCell = configCell;
        _sourceArray = sourceArray;
        _type = type;
        _reviseIndexPaths = reviseIndexPaths;
        _reviseReuses = reviseReuses;
        _configHeaderOrFooterView = configHeaderOrFooterView;
    }
    return self;
}

#pragma mark - 构造器
+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSource {
    return [[self alloc]initWithSourceArray:nil sourceArrayType:NormalCollectionViewTypeModel configCellBlock:nil];
}

+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSourceWithNormalType:(NormalCollectionViewType)type {
    return [[self alloc]initWithSourceArray:nil sourceArrayType:type configCellBlock:nil];
}

+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSourceWithNormalType:(NormalCollectionViewType)type configCellBlock:(ConfigCell)configCell {
    return [[self alloc]initWithSourceArray:nil sourceArrayType:type configCellBlock:configCell];
}

+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSourceWithNormalType:(NormalCollectionViewType)type configHeaderOrFooterView:(ConfigHeaderOrFooterView)configHeaderOrFooterView {
    return [[self alloc]initWithSourceArray:nil sourceArrayType:type reviseIndexPath:nil reviseReuses:nil configCellBlock:nil configHeaderOrFooterView:configHeaderOrFooterView];
}

+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSource:(NSArray *)sourceArray sourceArrayType:(NormalCollectionViewType)type configCellBlock:(ConfigCell)configCell {
    return [[self alloc]initWithSourceArray:sourceArray sourceArrayType:type configCellBlock:configCell];
}

+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSource:(NSArray *)sourceArray sourceArrayType:(NormalCollectionViewType)type reviseIndexPath:(NSArray<NSIndexPath *> *)reviseIndexPaths reviseReuses:(NSArray<NSString *> *)reviseReuses configCellBlock:(ConfigCell)configCell {
    return [[self alloc]initWithSourceArray:sourceArray sourceArrayType:type reviseIndexPath:reviseIndexPaths reviseReuses:reviseReuses configCellBlock:configCell configHeaderOrFooterView:nil];
}

+ (__kindof NormalCollectionViewDataSource *)NormalCollectionViewDataSource:(NSArray *)sourceArray sourceArrayType:(NormalCollectionViewType)type reviseIndexPath:(NSArray<NSIndexPath *> *)reviseIndexPaths reviseReuses:(NSArray<NSString *> *)reviseReuses configCellBlock:(ConfigCell)configCell configHeaderOrFooterView:(ConfigHeaderOrFooterView)configHeaderOrFooterView {
    return [[self alloc]initWithSourceArray:sourceArray sourceArrayType:type reviseIndexPath:reviseIndexPaths reviseReuses:reviseReuses configCellBlock:configCell configHeaderOrFooterView:configHeaderOrFooterView];
}

#pragma mark - 数据逻辑处理
- (UICollectionViewCell *)configCellTypeArrayWithCollectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *modelArray = _sourceArray[indexPath.section];
    NSString *reuse = [NSStringFromClass([modelArray[indexPath.item] class]) stringByAppendingString:@"CollectionCell"];
    if (_reviseIndexPaths != nil && [_reviseIndexPaths containsObject:indexPath]) {
        reuse = [_reviseReuses objectAtIndex:[_reviseIndexPaths indexOfObject:indexPath]];
    }
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    cell.model = modelArray[indexPath.item];
    return cell;
}

- (UICollectionViewCell *)configCellTypeModelWithCollectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseModel *model = _sourceArray[indexPath.item];
    NSString *reuse = [NSStringFromClass(model.class)stringByAppendingString:@"CollectionCell"];
    if (_reviseIndexPaths != nil && [_reviseIndexPaths containsObject:indexPath]) {
        reuse = [_reviseReuses objectAtIndex:[_reviseIndexPaths indexOfObject:indexPath]];
    }
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    cell.model = _sourceArray[indexPath.item];
    return cell;
}


#pragma mark - collection view dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _type == NormalCollectionViewTypeArray ? _sourceArray.count: 1;
}

// 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_sourceArray.count == 0) {
        return 0;
    } else {
        if (_type == NormalCollectionViewTypeArray) {
            if ([[_sourceArray objectAtIndex:section]isKindOfClass:[NSArray<BaseModel *> class]]) {
                return [[_sourceArray objectAtIndex:section] count];
            } else {
                NSLog(@"数组中存放的不是BaseModel");
                return 0;
            }
        } else {
            if ([_sourceArray isKindOfClass:[NSArray<BaseModel *> class]]) {
                return _sourceArray.count;
            } else {
                NSLog(@"数组中存放的不是BaseModel");
                return 0;
            }
        }
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_configCell == nil) {
        if (_type == NormalCollectionViewTypeArray) {
            UICollectionViewCell *cell = [self configCellTypeArrayWithCollectionView:collectionView cellForItemAtIndexPath:indexPath];
            return cell;
        } else {
            UICollectionViewCell *cell = [self configCellTypeModelWithCollectionView:collectionView cellForItemAtIndexPath:indexPath];
            return cell;
        }
    } else {
        BaseCollectionViewCell *cell = _configCell(collectionView, indexPath);
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return _configHeaderOrFooterView(collectionView, kind, indexPath);
}

@end
